﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang.BLL
{
    public class SupportsRepository
    {
        private QLGPMBEntities data = new QLGPMBEntities();

        public IEnumerable<Support> GetAll(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = from p in data.Supports
                          select p;
            totalRow = results.Count();
            return results.OrderBy(p => p.ProjectID).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        public Support GetByID(int ID)
        {
            var result = from p in data.Supports
                         where p.SuportID == ID
                         select p;
            return result.FirstOrDefault();
        }

        public Support GetByKey(int ProjectID, int HouseHoldID)
        {
            return data.Supports.FirstOrDefault(p => p.ProjectID == ProjectID && p.HouseHold.HouseHoldID == HouseHoldID);
        }
        public IEnumerable<Support> GetByCMND(string cmnd)
        {
            return data.Supports.Where(p => p.CMND == cmnd);
        }

        public IEnumerable<Support> GetByProjectID(int ID)
        {
            return data.Supports.Where(p => p.ProjectID == ID);
        }

        public void Insert(Support sp)
        {
            data.AddToSupports(sp);
        }

        public void DeleteByID(int id)
        {
            var obj = GetByID(id);
            if (obj != null)
            {
                data.DeleteObject(obj);
            }
        }

        public void Save()
        {
            data.SaveChanges();
        }
    }
}